package com.zhiyou.app.shop.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**  yyyy-MM-dd */
	public static final String DEFALUT_DATE_PATTERN="yyyy-MM-dd";
	
	private static final String simpleFormat = "yyyy-MM-dd HH:mm";
	
	/** 得到某天的开始时间 凌晨0点0分0秒 */
	public static Date getBeginOfDay(Date date){
		if(date==null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		Date ds = new Date(cal.getTimeInMillis());
		return ds;
	}
	
	public static Date getBeginDay(String startDate) {
		Date sd = convert2Date(DEFALUT_DATE_PATTERN, startDate);
		return getBeginOfDay(sd);
	}
	
	/** 得到某天的结束时间 凌晨23点59分59秒 */
	public static Date getEndOfDay(Date date) {
		if(date==null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		Date ds = new Date(cal.getTimeInMillis());
		return ds;
	}
	
	public static Date getEndDay(String endDate) {
		Date sd = convert2Date(DEFALUT_DATE_PATTERN, endDate);
		return getEndOfDay(sd);
	}
	
	/** 得到当月的第一天的开始 */
	public static Date getFirstDayOfCurMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);
		return getBeginOfDay(cal.getTime());
	}
	
	/** 得到当月的最后一天的结束 */
	public static Date getEndDayOfCurMonth(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return getEndOfDay(cal.getTime());
	}
	
	/** 得到上个月的第一天的开始 */
	public static Date getFirstDayOfLastMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -1);
		return getBeginOfDay(cal.getTime());
	}

	/** 得到上个月的最后一天的结束 */
	public static Date getLastDayOfLastMonth(){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return getEndOfDay(cal.getTime());
	}
	
	public static Date getThisSundayStart() {
		Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.SUNDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
	}
	
	public static Date getLastSundayStart() {
		Calendar cal = Calendar.getInstance();
        cal.setTime(getThisSundayStart());
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 7);
        return cal.getTime();
	}
	
	public static String simpleDateFormat(Date date){
		return formatDate(simpleFormat, date);
	}
	
	public static String formatDate(String format , Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);     
		return sdf.format(date);
	}
	
	public static Date convert2Date(String format, String date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getFirstDayOfCurMonthStr() {
		return DateUtil.formatDate(DEFALUT_DATE_PATTERN, DateUtil.getFirstDayOfCurMonth());
	}
	
	public static String getFirstDayOfLastMonthStr() {
		return DateUtil.formatDate(DEFALUT_DATE_PATTERN, DateUtil.getFirstDayOfLastMonth());
	}
	
	public static String getEndDayOfLastMonthStr(){
		return DateUtil.formatDate(DEFALUT_DATE_PATTERN, DateUtil.getLastDayOfLastMonth());
	}
	
	public static void main(String[] args) {
		System.out.println(convert2Date(DEFALUT_DATE_PATTERN, "2013-04-14").toLocaleString());
	}
}
